#include "input_stream.h"

InputStream::InputStream()
{
    av_log_set_level(avloglevel); 
    av_register_all();
    avformat_network_init();
}
bool InputStream::open_url(const char* url)
{
    this->m_url = url;
    AVDictionary *opts = NULL;
    int ret = 0;
    if(strstr(url,"rtsp") != NULL) {
        av_dict_set(&opts, "rtsp_transport", "tcp", 0);
    }
    ret = avformat_open_input(&m_fmt_ctx, url, NULL, &opts);
    if(ret < 0) {
        av_log(NULL, AV_LOG_ERROR, "Cannot open input file\n");
        return false;
    }

    if ((ret = avformat_find_stream_info(m_fmt_ctx, NULL)) < 0)
    {
        av_log(NULL, AV_LOG_ERROR, "Cannot find stream information\n");    
        return false;
    }

    // 查找视频流
    int video_stream_index = av_find_best_stream(m_fmt_ctx, AVMEDIA_TYPE_VIDEO    , -1, -1, NULL, 0);
    if (video_stream_index < 0) {
        fprintf(stderr, "Could not find video stream\n");
        return false;
    }
  
    // 查找音频流（如果存在）
    int audio_stream_index = av_find_best_stream(m_fmt_ctx, AVMEDIA_TYPE_AUDIO    , -1, -1, NULL, 0);
    int has_audio = audio_stream_index >= 0;
  
    m_video_stream = m_fmt_ctx->streams[video_stream_index];
    m_audio_stream = has_audio ? m_fmt_ctx->streams[audio_stream_index] : NULL;

    m_audio_stream_index = audio_stream_index;
    m_video_stream_index = video_stream_index;

    printf("open url success, video_stream_index:%d audio_stream_index:%d\n",video_stream_index,audio_stream_index);


    m_open_success = true;
    return true;
}

bool InputStream::has_audio()
{
    return m_audio_stream_index>-1;
}
void InputStream::close()
{
    if(m_fmt_ctx) {
        avformat_close_input(&m_fmt_ctx);
    }
}

int InputStream::read_stream(AVPacket* packet)
{
    int ret = 0;
    ret = av_read_frame(m_fmt_ctx, packet);
    // if(m_had_key_video_frame) {
    //     return ret;
    // }else {
    //     if(is_video(packet) && packet->flags == 1) {
    //         m_had_key_video_frame = true;
    //         return ret;
    //     }else {
    //         return -500; //skip
    //     }
    // }  
      
    return ret;
}
InputStream::~InputStream()
{
    if(m_open_success) {
        m_open_success = false;        
    }
}
 int InputStream::get_video_stream_info(AVCodecParameters* parameters, AVRational& time_base,int& index)
 {
    int ret = -1;
    if(m_video_stream) {
        memcpy(parameters,m_video_stream->codecpar,sizeof(AVCodecParameters));  
        time_base = m_video_stream->time_base;   
        ret = 0;   
    }
    index = m_video_stream_index;

    return 0;
 }

 int InputStream::get_audio_stream_info(AVCodecParameters* parameters, AVRational& time_base,int& index)
 {
    if(m_audio_stream) {
        memcpy(parameters,m_audio_stream->codecpar,sizeof(AVCodecParameters));  
        time_base = m_audio_stream->time_base;      
    }
    index = m_audio_stream_index;
    return 0;
 }
bool InputStream::is_audio(AVPacket* packet)
{
    return packet->stream_index == m_audio_stream_index;
}
bool InputStream::is_video(AVPacket* packet)
{
    return packet->stream_index == m_video_stream_index;
}